﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.Admin
{
    public partial class ListEnterpriseTop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["AdminLogin2"] != null)
                {
                    int atid = Convert.ToInt32(Session["AdminLogin2"]);

                    Bind(atid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Session["AdminLogin2"] != null)
            {
                int atid = Convert.ToInt32(Session["AdminLogin2"]);

                Bind(atid);
            }
        }

        protected void lbn_senEmail_Click(object sender, EventArgs e)
        {
            string emaillist = this.GetEmailAddlist();
            if (emaillist.Length > 0)
            {
                Session["receiveEmailAdd"] = emaillist;
                Response.Redirect("EmailSend.aspx");

            }
            else
            {
                //Maticsoft.Common.MessageBox.Show(this, "请选择所要发送邮件的单位！");
            }
        }

        protected void lbn_up_Click(object sender, EventArgs e)
        {
             string Idlist = this.GetSelIDlist();

             bool flag = false;

             if (!string.IsNullOrEmpty(Idlist))
             {
                 string [] idlists = Idlist.Split(',');
                 if (idlists.Length > 0)
                 {
                     
                     lcmyjobs.BLL.enterprise_top et = new BLL.enterprise_top();
                     foreach (string id in idlists)
                     {
                         if (et.UpdateTop(Convert.ToInt32(id), DateTime.Now, 1) > 0)
                         {
                             flag = true;
                         }
                         else
                         {
                             flag = false;
                         }
                     }
                     
                 }
                 if (flag)
                 {
                     //Maticsoft.Common.MessageBox.Show(this, "固定成功！");

                     if (Session["AdminLogin2"] != null)
                     {
                         int atid = Convert.ToInt32(Session["AdminLogin2"]);

                         Bind(atid);
                     }
                 }
                 
             }
        }

        protected void lbn_del_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_top eb = new BLL.enterprise_top();

                if (eb.DeleteList(Idlist))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "删除成功！");

                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要删除的单位！", "ListEnterpriseTop.aspx");
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_check")
            {
                if (e.CommandArgument != null)
                {
                    int topid = Convert.ToInt32(e.CommandArgument);

                    if (new lcmyjobs.BLL.enterprise_top().UpdateTop(topid,DateTime.Now,1)>0)
                    {
                        //Maticsoft.Common.MessageBox.Show(this, "固定成功！");

                        if (Session["AdminLogin2"] != null)
                        {
                            int atid = Convert.ToInt32(Session["AdminLogin2"]);

                            Bind(atid);
                        }
                    }
                }
            }
        }

        private void Bind(int atid)
        {
            int count = 0;

            DataSet ds = new lcmyjobs.BLL.enterprise_top().GetTopInfo(atid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            this.lbl_count.Text = ds.Tables[1].Rows[0][0].ToString();

            this.AspNetPager1.RecordCount = count;

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        
        }

        /// <summary>
        /// 获取主键ID列表
        /// </summary>
        /// <returns></returns>
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        /// <summary>
        /// 获取Email邮件列表
        /// </summary>
        /// <returns></returns>
        private string GetEmailAddlist()
        {
            string emaillist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_email") as Label;

                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        emaillist += lb.Text + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                emaillist = emaillist.Substring(0, emaillist.LastIndexOf(","));
            }
            return emaillist;
        }
        public string GetBH(string str)
        {
            return str.ToString().PadLeft(6, '0');
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[5].Text == "未固定" || e.Row.Cells[5].Text == "增加天数")
            {
                e.Row.Cells[5].ControlStyle.ForeColor = System.Drawing.Color.Red;
            }
        }

       
    }
}